<template>
  <div class="app-container">
    <el-card style="margin-bottom: 20px;">
      <div class="box-card" style="width: 100%;  display: flex; justify-content: space-between; align-items: center">
        <el-input v-model="search" placeholder="请输入内容" />
        <el-button type="success" @click="likeQuery">搜索</el-button>
        <el-button type="success" @click="addCourse">添加</el-button>
      </div>
    </el-card>
    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%">
      <el-table-column align="center" label="ID" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column width="180px" align="center" label="课程名称">
        <template slot-scope="scope">
          <span>{{ scope.row.course_name }}</span>
        </template>
      </el-table-column>

      <el-table-column width="765px" align="center" label="课程描述">
        <template slot-scope="scope">
          <span>{{ scope.row.description }}</span>
        </template>
      </el-table-column>

      <el-table-column width="100px" label="授课老师">
        <template slot-scope="scope">
          <span>{{ scope.row.teacher }}</span>
        </template>
      </el-table-column>

      <el-table-column class-name="status-col" label="课程封面" width="110">
        <template slot-scope="scope">
          <!--          <span>{{ scope.row.course_image }}</span>-->
          <el-image v-if="scope.row.course_image !== null" :src="scope.row.course_image" />
        </template>
      </el-table-column>
      <el-table-column width="257px" label="创建时间">
        <template slot-scope="scope">
          <span>{{ scope.row.created_at }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="Actions" width="120">
        <template slot-scope="scope">
          <el-button type="primary" size="small" icon="el-icon-edit" @click="editCourse(scope.row)">
            编辑
          </el-button>
        </template>
      </el-table-column>
      <el-table-column align="center" label="Actions" width="120">
        <template slot-scope="scope">
          <el-button type="danger" size="small" icon="el-icon-delete" @click="deleteCourse(scope.row.id)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <DragDialog ref="test" :title="'添加课程'">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="课程名称">
          <el-input v-model="form.course_name" />
        </el-form-item>
        <el-form-item label="授课老师">
          <el-input v-model="form.teacher" type="text" />
        </el-form-item>
        <el-form-item label="课程描述">
          <el-input v-model="form.description" type="textarea" />
        </el-form-item>
        <el-form-item label="课程封面">
          <el-input v-model="form.course_image" type="text" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit(button,)">{{ button }}</el-button>
          <el-button @click="hideDialog">取消</el-button>
        </el-form-item>
      </el-form>
    </DragDialog>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'; // Secondary package based on el-pagination
import Resource from '@/api/resource';
import DragDialog from '@/views/components-demo/DragDialog.vue';
const courseResource = new Resource('course');
// const courseCreate = new Resource('course/create');
// import request from '@/utils/request';
export default {
  name: 'Course',
  components: { Pagination, DragDialog },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger',
      };
      return statusMap[status];
    },
  },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10,
      },
      search: '',
      dialogVisible: false,
      form: {
        course_name: '',
        teacher: '',
        description: '',
        course_image: '',
      },
      button: '立即添加',
    };
  },
  watch: {
    search(val, oldVal){ // 普通的watch监听
      // console.log('search: ' + val, oldVal);
      if (val === ''){
        this.getList();
      }
    },
  },
  created() {
    this.getList();
  },
  methods: {
    async getList() {
      this.listLoading = true;
      const { data } = await courseResource.list(this.listQuery);
      // console.log(data);
      this.list = data.items.data;
      this.total = data.items.total;
      this.listLoading = false;
    },
    async likeQuery(){
      const { data } = await courseResource.likeQuery(this.search);
      console.log(data.items);
      this.list = data.items;
      this.total = data.items.length;
    },
    hideDialog() {
      this.$refs.test.hideDialog();
    },
    addCourse(){
      this.form = {};
      this.button = '立即添加';
      this.$refs.test.showDialog();
    },
    async onSubmit(id){
      this.$refs.test.hideDialog();
      // const data = await courseResource.store(this.form);
      const data = this.button === '立即添加' ? await courseResource.store(this.form) : await courseResource.update(this.id, this.form);
      console.log(data);
      this.$message({
        message: this.form.course_name + data,
        type: 'success',
      });
      this.getList();
    },
    async deleteCourse(id){
      const data = await courseResource.destroy(id);
      this.$message.error(data);
      this.getList();
    },
    async editCourse(row){
      this.button = '立即修改';
      this.form = {
        course_name: row.course_name,
        teacher: row.teacher,
        description: row.description,
        course_image: row.course_image,
      };
      this.id = row.id;
      this.$refs.test.showDialog();
    },
  },
};
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
